package com.joker.airoleplayplatformback.config;


import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiEmbeddingModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.qdrant.QdrantEmbeddingStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

/**
 * 语言模型配置
 * @author : feixiang.li
 * @since : 2025-09-26 21:23
 */
@Configuration
public class LLMConfig {

    @Value("${langchain4j.open-ai.chat-model.api-key}")
    private String apiKey;

    @Value("${langchain4j.open-ai.chat-model.base-url}")
    private String apiUrl;


    /**
     * 向量模型的apiKey
     */
    @Value("${embedding.api-key}")
    private String aliApiKey;

    /**
     * 向量模型
     */
    @Value("${embedding.base-url}")
    private String aliApiUrl;

    @Value("${qdrant.host}")
    private String qdrantHost;
    @Value("${qdrant.port}")
    private int qdrantPort;


    @Bean
    public ChatModel chatModel() {
        return OpenAiChatModel.builder()
                .apiKey(apiKey)
                .baseUrl(apiUrl)
                .modelName("qwen3-max")
                .timeout(java.time.Duration.ofSeconds(60))
                .temperature(0.7)
                .logRequests(true)
                .logResponses(true)
                .build();
    }

    @Bean
    public StreamingChatModel streamingChatModel() {
        return OpenAiStreamingChatModel.builder()
                .apiKey(aliApiKey)
                .baseUrl(apiUrl)
                .modelName("qwen3-max")
                .timeout(Duration.ofSeconds(60))
                .temperature(0.7).logRequests(true)
                .logResponses(true)
                .build();
    }


    @Bean
    public EmbeddingModel embeddingModel() {
        return OpenAiEmbeddingModel.builder()
                .apiKey(aliApiKey)
                .baseUrl(aliApiUrl)
                .modelName("text-embedding-v4")
                .timeout(Duration.ofSeconds(60))
                .build();
    }


    @Bean
    public EmbeddingStore<TextSegment> embeddingStore() {
        return QdrantEmbeddingStore.builder()
                .host(qdrantHost)
                .port(qdrantPort)
                .collectionName("my-qiniu")
                .build();
    }
}
